define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'livestreamer/index/index' + location.search,
                    add_url: 'livestreamer/index/add',
                    // edit_url: 'livestreamer/index/edit',
                    // del_url: 'livestreamer/index/del',
                    multi_url: 'livestreamer/index/multi',
                    import_url: 'livestreamer/index/import',
                    table: 'livestreamer_list',
                    artific_url: 'livestreamer/index/artific',
                    manual_url: 'livestreamer/index/manual',
                }
            });

            var table = $("#table");
            $('.btn-artific').data('area',['85%','85%']);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'livestreamerUser',
                sortName: 'checkTime',
                fixedColumns: true,
                fixedRightNumber: 1,
                templateView: false,
                clickToSelect: true,
                visible: false,
                //search: true,
                showColumns: false,
                showToggle: false,
                showExport: true,
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'livestreamerUser', title: __('Livestreameruser'), operate: 'LIKE'},
                        {field: 'gender', title: __('Gender'), searchList: {"0":__('Unknown'),"1":__('Male'),"2":__('Female'),"3":__('Neutral')}, formatter: Table.api.formatter.normal},
                        {field: 'age', title: __('Age')},
                        {field: 'faceLevel', title: __('Facelevel')},
                        {field: 'fansNum', title: __('Fansnum')},
                        {field: 'language', title: __('Language')},
                        {field: 'region', title: __('Region')},
                        {field: 'videoNum', title: __('VideoNum')},
                        {field: 'spiderType', title: __('SpiderType')},
                        {field: 'contactType', title: __('Contacttype'), searchList: {"0":__('Contacttype 0'),"1":__('Contacttype 1'),"2":__('Contacttype 2'),"3":__('Contacttype 3'),"4":__('Contacttype 4'),"5":__('Contacttype 5')}, formatter: Table.api.formatter.normal},
                        //{field: 'contact', title: __('Contact'), operate: 'LIKE'},
                        {field: 'checkStatus', title: __('Checkstatus'), searchList: {"-1":__('Not verified'),"0":__('Unavailable'),"1":__('Verified (contractable)')}, formatter: Table.api.formatter.status},
                        {field: 'checkRemark', title: __('Checkremark'), operate: 'LIKE'},
                        {field: 'checkTime', title: __('Checktime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'grade', title: __('Grade'), searchList: {"-1":__('GradeUnRated'),"0":__('GradeAbandon'),"1":__('GradeQualified'),"2":__('GradeExcellent')}, formatter: Table.api.formatter.normal},
                        {field: 'signature', title: __('Signature')},
                        {field: 'spiderRobotUser', title: __('Spiderrobotuser'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 爬虫机器人联查
            $(document).on("change","#c-agent",function(e){
                var agent = $(this).val();
                if (agent=='') {
                    $('#c-robot').html('<option value>__(\'Please select a broker first\')</option>');
                    return false;
                }
                Fast.api.ajax({
                    url: 'livestreamer/index/checkrobot',
                    data: {'agent':agent}
                }, function (data) {
                    if (data.html) {
                        $('#c-robot').html(data.html);
                    }else{
                        $('#c-robot').html('<option value>__(\'Please select a broker first\')</option>');
                        Toastr.error('无爬虫机器人');
                        return false;
                    }
                })
            });
            //查询
            $(document).on('click','.btn-checkx',function(){
                // var agent = $('#c-agent').val();
                // var robot = $('#c-robot').val();
                // var checkStatus = $('#c-checkStatus').val();
                // var crawltime = $('#c-crawltime').val();
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    params.isd = 'w';
                    console.log(params);
                    return params;
                }
                console.log(options);
                return false;
                Fast.api.ajax({
                    url: 'livestreamer/index/checkzhubo',
                    data: {'agent':agent,'robot':robot,'checkStatus':checkStatus,'crawltime':crawltime}
                }, function (data) {

                });
            });
            // 人工复检按钮事件
            $(document).on('click', '.btn-artific', function () {
                $(this).data('area',['95%','90%']);
                var ids = Table.api.selectedids(table);
                var url = 'livestreamer/index/artific';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            });
            // 手工添加按钮事件
            $(document).on('click', '.btn-manual', function () {
                var ids = Table.api.selectedids(table);
                var url = 'livestreamer/index/manual';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        artific: function () {
            // 爬虫机器人联查
            $(document).on("change","#c-agent",function(e){
                var agent = $(this).val();
                if (agent=='') {
                    $('#c-robot').html('<option>__(\'Please select a broker first\')</option>');
                    return false;
                }
                Fast.api.ajax({
                    url: 'livestreamer/index/checkrobot',
                    data: {'agent':agent}
                }, function (data) {
                    if (data.html) {
                        $('#c-robot').html(data.html);
                    }else{
                        $('#c-robot').html('<option>__(\'Please select a broker first\')</option>');
                        Toastr.error('无爬虫机器人');
                        return false;
                    }
                })
            });
            $(document).on("click",".btn-check",function(e){
                e.preventDefault();
                var page = $(this).data('page')?$(this).data('page'):1;
                // var agent = $('#c-agent').val();
                var robot = $('#c-robot').val();
                var gender = $('#c-gender').val();
                var checkStatus = $('#c-checkStatus').val();
                var channel = $('#c-channel').val();
                var grade = $('#c-grade').val();
                var region = $('#c-region').val();
                var fansNum = '';
                // if ($('input[name="fansNum[1]"]').val()!='' && $('input[name="fansNum[2]"]').val()!='') {
                //     fansNum = $('input[name="fansNum[1]"]').val()+','+$('input[name="fansNum[2]"]').val();
                // }
                // var videoNum = '';
                // if ($('input[name="videoNum[1]"]').val()!='' && $('input[name="videoNum[2]"]').val()!='') {
                //     videoNum = $('input[name="videoNum[1]"]').val()+','+$('input[name="videoNum[2]"]').val();
                // }
                var order = $('#c-order').val();
                // if (!robot) {
                //     Toastr.error('请选择条件后查询');
                //     return false;
                // }
                Fast.api.ajax({
                    url: 'livestreamer/index/checkzhubo',
                    data: {'page':page,'robot':robot,'gender':gender,'channel':channel,'checkStatus':checkStatus,'grade':grade,'region':region,'order':order}
                }, function (data) {
                    $('#zhubolist').html(data.html);
                    if (data.totalpage>1 && data.totalpage>data.pages) {
                        $('.prev').attr('disabled',false);
                        $('.next').attr('disabled',false);
                        $('.prev').data('page',data.pages==1?Number(data.pages):Number(data.pages)-1);
                        $('.next').data('page',Number(data.pages)+1);
                        $('.next').addClass('btn-check');
                    }else{
                        $('.next').removeClass('btn-check');
                        $('.next').attr('disabled',true);
                    }
                    if (data.pages==1) {
                        $('.prev').removeClass('btn-check');
                        $('.prev').attr('disabled',true);
                    }else{
                        $('.prev').attr('disabled',false);
                    }
                    if (data.pages>1) {
                        $('.prev').addClass('btn-check');
                    }

                    //初始化复核数据
                    $('#videolist').html('');
                    $('#livestreamerUser').val('');
                    $('#avatar').attr('src','');
                    $('#nickname').html('');
                    $('#followNum').html('');
                    $('#fansNum').html('');
                    $('#likesNum').html('');
                    $('#signature').html('');
                    $('#faceLevel').val('');
                    $('#newVideoTime').val('');
                    $('#c-contact_type').val('');
                    $('#c-contact').val('');
                    //console.log(data);
                    //Fast.api.close();
                    // parent.Toastr.success('操作成功');
                    // parent.$(".btn-refresh").trigger("click");
                });
            });
            //行事件
            $(document).on("click","#zhubolist .kts",function(e){
                var uid = $(this).data('id');
                $('#zhubolist .kts').removeClass('hover');
                $(this).addClass('hover');
                Fast.api.ajax({
                    url: 'livestreamer/index/checkZhuboDetail',
                    data: {uid}
                }, function (data) {
                    //console.log(data);
                    if(data.html!=''){
                       $('#videolist').html(data.html);
                    }
                    $('#livestreamerUser').val(data.userinfo.livestreamerUser);
                    $('#avatar').attr('src',data.userinfo.photourl);
                    $('#nickname').html(data.userinfo.nickname);
                    $('#followNum').html(data.userinfo.followNum);
                    $('#fansNum').html(data.userinfo.fansNum);
                    $('#likesNum').html(data.userinfo.likesNum);
                    $('#signature').html(data.userinfo.signature);
                   // $('#videoNum').val(data.userinfo.videoNum);
                    $('#faceLevel').val(data.userinfo.faceLevel);
                    $('#newVideoTime').val(data.userinfo.newVideoTime);
                    $('#c-contact_type').val(data.userinfo.contactType);
                    $('#c-contact').val(data.userinfo.contact);
                    // $('#firstVideoTime').val(data.userinfo.firstVideoTime);
                    //Fast.api.close();
                    // parent.Toastr.success('操作成功');
                    // parent.$(".btn-refresh").trigger("click");
                });
            });
            //弹出视频事件
            $(document).on("click","#videolist img",function(e){
                var img = $(this).attr('src');//获取视频预览图
                var video = $(this).data('vurl');//获取视频路径
                if(video){
                    $('.videos').html("<video id=\"video\" poster='"+img+"' style='width: 480px,heigth: 720px,' src='"+video+"' preload=\"auto\" controls=\"controls\" autoplay=\"autoplay\"></video>");
                    $('.videos').show();
                    $('.mask').show();
                }else{
                    Toastr.error('无视频信息');
                    return false;
                }

            });
            $(document).on("click",".mask",function(e){
                var v = document.getElementById('video');//获取视频节点
                $('.videos').hide();//点击关闭按钮关闭暂停视频
                v.pause();
                $('.videos').html('');
                $('.mask').hide();
            });
            //保存人工审核
            $(document).on("click",".btn-save",function(e){
                var livestreamerUser = $('#livestreamerUser').val();
                var grade = $("input[name='grade']:checked").val();
                var checkRemark = $('#c-checkRemark').val();
                var contractType = $('#c-contact_type').val();
                var contract = $('#c-contact').val();
                if (livestreamerUser=="") {
                    Toastr.error('请选择要审核的主播');
                    return false;
                }
                Fast.api.ajax({
                    url: 'livestreamer/index/artific',
                    data: {'livestreamerUser':livestreamerUser,'grade':grade,'checkRemark':checkRemark,'contractType':contractType,'contract':contract}
                }, function (data) {
                    setTimeout(function(){
                        document.getElementById('btn-check').click();
                    },2000);
                    //Toastr.success('操作成功');

                });
                //parent.Toastr.success('审核成功');
            });


            Controller.api.bindevent();
        },
        manual: function () {
            $(".btn-checkuser").on("click",function(e){
                e.preventDefault();
                if ($('#c-zhubo').val()=='') {
                   Toastr.error('请填写主播数据');
                   return false;
                }
                var zhuboarr = $('#c-zhubo').val().split(/[\r\n]+/);
                Fast.api.ajax({
                    url: 'livestreamer/index/checkuser',
                    data: {zhuboarr}
                }, function (data) {
                    //console.log(data);
                    $('#zhubolist').html(data.html);
                    $('#zblist').val(data.arr);
                    //Fast.api.close();
                    // parent.Toastr.success('操作成功');
                    // parent.$(".btn-refresh").trigger("click");
                });
            });
            $(".btn-ok").on("click",function(e){
                var userlist = $('#zblist').val();
                if (userlist=='') {
                   Toastr.error('请添加主播数据');
                   return false;
                }
                Fast.api.ajax({
                    url: 'livestreamer/index/manual',
                    data: {'signAgentID':$('#c-agent').val(),'userlist':eval('(' + userlist + ')')}
                }, function (data) {
                    //console.log(data);
                    Fast.api.close();
                    parent.Toastr.success('操作成功');
                    parent.$(".btn-refresh").trigger("click");
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
